﻿


"use strict";
define(['admin/app', "service/admin/logisticsService"], function (app) {
    app.controller('logisticsController', function ($scope, $stateParams, $location, $uibModal, $state, utils, logisticsService) {
        $scope.query = {
            page: 1, size: 20,  flight_no: '', board_no: '',  logistics_status: ''
        };
        $scope.query1 = {
            page: 1, size: 20
        };
        $scope.banid = "";
        $scope.ids = "";
        $scope.plateids = "";
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                logisticsService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
           
            getlist: function () {
                logisticsService.getlist({ id: $stateParams.id }).success(function (response) {
                    $scope.model = response.data;
                });
            },
            get: function () {
                if ($stateParams.id > 0) {
                    logisticsService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            check: function ($event, id, weight) {
                var w = 0;
                var checkbox = $event.target;
                if (checkbox.checked) {
                    w = w + weight;
                    if ($scope.ids == "") {
                        $scope.ids = id + ",";
                    } else {
                        $scope.ids = $scope.ids + id+",";
                    }
                }

                var storage = window.localStorage;
                storage.setItem("ids", $scope.ids);
               
            },
            check1: function ($event, id ) {
              
                var checkbox = $event.target;
                if (checkbox.checked) {
                  
                    if ($scope.plateids == "") {
                        $scope.plateids = id + ",";
                    } else {
                        $scope.plateids = $scope.plateids  + id+",";
                    }
                }

                var storage = window.localStorage;
                storage.setItem("plateids", $scope.plateids);
                
            },

            
            finishdeclare: function () {
                logisticsService.finishdeclare({ id: $stateParams.id }).success(function (response) {
                    $scope.model = response.data;
                });
            },
            fillsingle: function () {
                var storage = window.localStorage;
           
                logisticsService.fillsingle({ ids: storage.getItem("ids") }).success(function (response) {
                    $scope.model = response.data;
                   
                    
                });
            },
            startflight: function () {
                logisticsService.startflight({ id: $stateParams.id }).success(function (response) {
                    $scope.model = response.data;
                });
            },
            save: function () {
                var model = $scope.model;
                logisticsService.save({ list: model }).success(function (response) {
                    $scope.model = response.data;
                    methods.goBack();
                });
            },
            saveboard: function () {
                var model = $scope.model;
                var storage = window.localStorage;
                logisticsService.saveboard({ boardname: model.boardname, Ids: storage.getItem("ids") }).success(function (response) {
                    utils.notify(response.data.result_msg, response.result_code == 1000 ? "success" : "warning");
                    // $scope.model = response.data;
                    if (response.result_code == 1000)
                        location.href = "/#/app/logistics/list";
                });
               
            },
            adds: function () {
                var model = $scope.model;
                var storage = window.localStorage;
                logisticsService.add({ Ids: storage.getItem("ids"), logid: storage.getItem("logid") }).success(function (response) {
                    utils.notify(response.data.result_msg, response.result_code == 1000 ? "success" : "warning");
                    // $scope.model = response.data;
                    if (response.result_code == 1000)
                        storage.setItem("logid", "");
                        location.href = "/#/app/logistics/list";
                });
            },
            saveplate: function () {
                var model = $scope.model;
                var storage = window.localStorage;

                logisticsService.saveplate({ Id: model[0].boardid, boardname: model.boardname, length: model.length, width: model.width, height: model.height, Ids: storage.getItem("plateids") }).success(function (response) {
                    utils.notify(response.data.result_msg, response.result_code == 1000 ? "success" : "warning");
                    methods.splitplatelist();
                    if (response.result_code == 1000)
                        location.href = "/#/app/logistics/splitplatelist";
                });

            },
            paylist:function() {
                logisticsService.paylist().success(function (response) {
                    var arry = new Array();
                    if (response.data != null && response.data.length > 0) {
                        for (var index = 0; index < response.data.length; index++) {
                            if (response.data[index].orders != null && response.data[index].orders.length > 0) {
                              
                                if (response.data[index].orders[0].status == "3") {
                                    var count = 0;
                                    for (var product = 0; product < response.data[index].orders.length; product++) {
                                        if (response.data[index].orders[product].order_items != null && response.data[index].orders[product].order_items.length > 0) {
                                            for (var item = 0; item < response.data[index].orders[product].order_items.length; item++) {
                                                count = count + response.data[index].orders[product].order_items[item].quantity * response.data[index].orders[product].order_items[item].product.weight;
                                            }
                                        }
                                    }
                                    var payinfo = {
                                        id: response.data[index].id,
                                        order_no: response.data[index].order_no,
                                        weight: count,
                                        cretetime: response.data[index].pay_time
                                    };
                                    arry.push(payinfo);
                                  
                                }

                            }
                        }
                    }
                   
                    $scope.model = arry;
                   
                });
            },
            paylist1: function () {
              
                logisticsService.paylist().success(function (response) {
                    var arry = new Array();
                    if (response.data != null && response.data.length > 0) {
                        for (var index = 0; index < response.data.length; index++) {
                            if (response.data[index].orders != null && response.data[index].orders.length > 0) {

                                if (response.data[index].orders[0].status == "3") {
                                    var count = 0;
                                    for (var product = 0; product < response.data[index].orders.length; product++) {
                                        if (response.data[index].orders[product].order_items != null && response.data[index].orders[product].order_items.length > 0) {
                                            for (var item = 0; item < response.data[index].orders[product].order_items.length; item++) {
                                                count = count + response.data[index].orders[product].order_items[item].quantity * response.data[index].orders[product].order_items[item].product.weight;
                                            }
                                        }
                                    }
                                    var payinfo = {
                                        logiticsid: $stateParams.id ,
                                        id: response.data[index].id,
                                        order_no: response.data[index].order_no,
                                        weight: count,
                                        cretetime: response.data[index].pay_time
                                    };
                                    arry.push(payinfo);

                                }

                            }
                        }
                    }

                    $scope.model = arry;

                });
            },
            fillsplitplate: function () {
                var storage = window.localStorage;

                logisticsService.fillsplitplate({ ids: storage.getItem("plateids") }).success(function (response) {
                    $scope.model = response.data;
                  
                });
            },
            saveno: function () {
                var model = $scope.model;
                logisticsService.saveno({ model: model }).success(function (response) {
                    $scope.model = response.data;
                    methods.goBack();
                });
            },
            add: function () {
                var model = $scope.model;
              
                logisticsService.saveno({ model: model }).success(function (response) {
                    $scope.model = response.data;
                    methods.goBack();
                });
            },
            update: function (id, status) {
                logisticsService.update({ id: id, status:status }).success(function (response) {
                    $scope.model = response.data;
                    methods.splitplatelist();
                });
            },
            splitplate: function () {
                logisticsService.splitplate({ id: $stateParams.id }).success(function (response) {
                    $scope.model = response.data;
                  
                });
            },
            splitplatelist: function () {
                logisticsService.splitplatelist($scope.query1).success(function (response) {
                    $scope.model = response.data;
                });
            },
            status_name: function (status) {
                if (status == 1) return "备货";
                else if (status == 2) return "提单";
                else if (status == 3) return "分板";
                else if (status == 4) return "运单航班";
                else if (status == 5) return "空运航班抵达目的地";
                else if (status == 6) return "三单推送";
                else if (status == 7) return "到库分栋，开始清关";
                else if (status == 8) return "清关完成开始配送";
                else if (status == 9) return "国内物流";
              
                else return "异常";
            },
            tidan: function () {
                logisticsService.tidan({ id: $stateParams.id }).success(function (response) {
                    var strorg = window.localStorage;
                    if (response.data.length > 0) {
                        $scope.banid = response.data[0].boardid;
                        strorg.setItem("logid", response.data[0].boardid);

                        }
                    $scope.model = response.data;

                });
            },
           
            delete: function (id) {
                logisticsService.delete({ id: id, }).success(function (response) {
                    $scope.model = response.data;
                    methods.tidan();
                  
                });
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
            searchplate_page: function (page) {
                $scope.query1.page = page;
                methods.search('page', $scope.query1.page);
            },
        }

        angular.extend($scope, methods);
        if ($state.includes('app.logistics.detail')) {
            methods.getlist();
        }
        else if ($state.includes('app.logistics.finishdeclare'))
        {
            methods.finishdeclare();
        }
        else if ($state.includes('app.logistics.startflight'))
        {
            methods.startflight();
        }
        else if ($state.includes('app.logistics.select')) {
          
            methods.paylist();
        } else if ($state.includes('app.logistics.fillsingle')) {
          
            methods.fillsingle();
        }
        else if ($state.includes('app.logistics.splitplate')) {
          
            methods.splitplate();
        }
        else if ($state.includes('app.logistics.fillsplitplate')) {

            methods.fillsplitplate();
        }
        else if ($state.includes('app.logistics.splitplatelist')) {
            methods.splitplatelist();
        }
        else if ($state.includes("app.logistics.tidan")) {
            methods.tidan();
        }
        else if ($state.includes("app.logistics.addpayinfo")) {
          
            methods.paylist1();
        }
        else {
        	methods.search();
    	}
    });
});
